package com.runfast.waimai.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.mapper.RunfastGoodsSellMapper;
import com.runfast.waimai.dao.model.*;
import com.runfast.waimai.service.*;
import com.runfast.waimai.utils.ComUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class RunfastGoodsSellServiceImpl extends BaseService<RunfastGoodsSell, Integer, RunfastGoodsSellExample> implements RunfastGoodsSellService {
    @Autowired
    private RunfastGoodsSellStandardService runfastGoodsSellStandardService;
    @Autowired
    private RunfastGoodsSellOptionService runfastGoodsSellOptionService;
    @Autowired
    private RunfastGoodsSellSubOptionService runfastGoodsSellSubOptionService;
    @Autowired
    private RunfastGoodsSellService runfastGoodsSellService;
    @Autowired
    private RunfastActivityTargetService runfastActivityTargetService;
    public List<RunfastGoodsSell> setStandardList(List<RunfastGoodsSell> goodsSellList){
        if(goodsSellList.size()>0){
            for(RunfastGoodsSell goodsSell:goodsSellList){
                RunfastGoodsSellStandardExample standardExample=new RunfastGoodsSellStandardExample();
                standardExample.createCriteria().andGoodssellidEqualTo(goodsSell.getId()).andBusinessidEqualTo(goodsSell.getBusinessid()).andDeletedEqualTo(true);
                List<RunfastGoodsSellStandard> standardList=runfastGoodsSellStandardService.selectByExample(standardExample);
                goodsSell.setStandardList(standardList);
            }
        }
        return goodsSellList;
    }
    public List<RunfastGoodsSell> setOptionList(List<RunfastGoodsSell> goodsSellList){
        if(goodsSellList.size()>0){
            for(RunfastGoodsSell goodsSell:goodsSellList){
                RunfastGoodsSellOptionExample optionExample=new RunfastGoodsSellOptionExample();
                optionExample.createCriteria().andGoodssellidEqualTo(goodsSell.getId()).andBusinessidEqualTo(goodsSell.getBusinessid()).andDeletedEqualTo(true);
                List<RunfastGoodsSellOption> optionList=runfastGoodsSellOptionService.selectByExample(optionExample);
                if(optionList.size()>0){
                    for(RunfastGoodsSellOption option:optionList){
                        RunfastGoodsSellSubOptionExample example=new RunfastGoodsSellSubOptionExample();
                        example.createCriteria().andOptionidEqualTo(option.getId()).andGoodssellidEqualTo(option.getGoodssellid()).andBusinessidEqualTo(option.getBusinessid()).andDelEqualTo(1);
                        List<RunfastGoodsSellSubOption> sellSubOptionList=runfastGoodsSellSubOptionService.selectByExample(example);
                        option.setSellSubOptionList(sellSubOptionList);
                    }
                }
                goodsSell.setOptionList(optionList);
            }
        }
        return goodsSellList;
    }
    /**更新主商品表里num字段*/
    public void updateNum(RunfastGoodsSell goodsSell){
        RunfastGoodsSellStandardExample standardExample=new RunfastGoodsSellStandardExample();
        standardExample.createCriteria().andGoodssellidEqualTo(goodsSell.getId()).andBusinessidEqualTo(goodsSell.getBusinessid()).andDeletedNotEqualTo(true);
        List<RunfastGoodsSellStandard> standardList=runfastGoodsSellStandardService.selectByExample(standardExample);
        Integer total=0;
        if(standardList.size()>0){
           for(RunfastGoodsSellStandard standard:standardList){
               total=total+standard.getNum();
           }
        }
        RunfastGoodsSell goodsSell1=new RunfastGoodsSell();
        goodsSell1.setId(goodsSell.getId());
        goodsSell1.setNum(total);
        runfastGoodsSellService.updateByPrimaryKeySelective(goodsSell1);
    }


    public Result update(RunfastGoodsSell t){
        RunfastGoodsSell goodsSell=runfastGoodsSellService.selectByPrimaryKey(t.getId());
        if(goodsSell==null){return Result.fail(null,"对应的商品已删除或不存在！");}
        Map<Object,Object> map=selectBygoodsId(t);
        List<RunfastGoodsSellStandard> standards=(List<RunfastGoodsSellStandard>)map.get("standards");
        List<RunfastGoodsSellOption> optionList=(List<RunfastGoodsSellOption>)map.get("optionList");
        Iterator<RunfastGoodsSellOption> iteratorOption=optionList.iterator();
        //保存规格
        if(t.getStandardList().size()>0){
            for(RunfastGoodsSellStandard s :t.getStandardList()){
                if(s.getId()!=null){
                    if(standards.size()>0){
                        int i=0;
                        for(RunfastGoodsSellStandard standard:standards){
                             if(s.getId().equals(standard.getId())){
                                 standards.remove(i);
                                 break;
                             }
                             i++;
                        }
                    }
                    if(s.getPrice()!=null &&!ComUtil.isEmpty(s.getName())){
                        //保存
                        s.setGoodssellid(goodsSell.getId());
                        s.setBusinessid(goodsSell.getBusinessid());
                        s.setBusinessname(goodsSell.getBusinessname());
                        runfastGoodsSellStandardService.updateByPrimaryKeySelective(s);
                    }
                }else if(s.getId()==null && s.getPrice()!=null &&!ComUtil.isEmpty(s.getName())){
                    //id不存在，新添加的规格
                    s.setBusinessid(goodsSell.getBusinessid());
                    s.setGoodssellid(goodsSell.getId());
                    s.setBusinessname(goodsSell.getBusinessname());
                    if(s.getFullNum()==null){
                        s.setFullNum(false);
                    }
                    s.setDeleted(false);
                    runfastGoodsSellStandardService.insertSelective(s);
                }
            }
        }
        if(standards.size()>0){
              //数据库有，而前端传过来的没有，删除掉
              for(RunfastGoodsSellStandard standard:standards){
                  standard.setDeleted(true);
                  runfastGoodsSellStandardService.updateByPrimaryKeySelective(standard);
              }
        }
        //保存属性
        if(t.getOptionList().size()>0){
             for(RunfastGoodsSellOption option:t.getOptionList()){
                 if(option.getId()!=null){
                     if(optionList.size()>0){
                         while (iteratorOption.hasNext()) {
                             RunfastGoodsSellOption option1=iteratorOption.next();
                             if(option.getId().equals(option1.getId())){
                                 option1.setName(option.getName());
                                 if(option1.getDeleted()!=null && !option1.getDeleted()  &&!ComUtil.isEmpty(option1.getName())){
                                     //更新
                                     runfastGoodsSellOptionService.updateByPrimaryKeySelective(option1);
                                     //数据库里的子属性
                                     List<RunfastGoodsSellSubOption> subOptionList1=option1.getSellSubOptionList();
                                     Iterator<RunfastGoodsSellSubOption> iterator=subOptionList1.iterator();

                                     //前台传入的子属性
                                     List<RunfastGoodsSellSubOption> subOptionList=option.getSellSubOptionList();
                                     if(subOptionList.size()>0){
                                         for(RunfastGoodsSellSubOption sub:subOptionList){
                                             if(sub.getId()!=null){
                                                      while (iterator.hasNext()) {
                                                          RunfastGoodsSellSubOption sub1 = iterator.next();
                                                          if(sub.getId().equals(sub1.getId())){
                                                              sub1.setName(sub.getName());
                                                              if(sub1.getDel()!=1 && StringUtils.isNotBlank(sub1.getName())){
                                                                  //更新
                                                                  runfastGoodsSellSubOptionService.updateByPrimaryKeySelective(sub1);
                                                              }
                                                              iterator.remove();
                                                              break;
                                                          }
                                                      }
                                             }else if(sub.getId()==null && StringUtils.isNotBlank(sub.getName())){
                                                 //id为空，新增加的属性
                                                 sub.setBusinessid(goodsSell.getBusinessid());
                                                 sub.setGoodssellid(goodsSell.getId());
                                                 sub.setGoodssellname(goodsSell.getName());
                                                 sub.setOptionid(option.getId());
                                                 sub.setOptionname(option.getName());
                                                 sub.setBusinessname(goodsSell.getBusinessname());
                                                 sub.setDel(0);
                                                 runfastGoodsSellSubOptionService.insert(sub);
                                             }
                                         }
                                     }
                                     if(subOptionList1.size()>0){
                                         for(RunfastGoodsSellSubOption subOption:subOptionList1){
                                             subOption.setDel(1);
                                             runfastGoodsSellSubOptionService.updateByPrimaryKeySelective(subOption);
                                         }
                                     }
                                 }
                                 iteratorOption.remove();
                                 break;
                             }
                         }
                     }
                 }else if(option.getId()==null && !ComUtil.isEmpty(option.getName())){
                     //id不存在，新添加的属性
                     option.setBusinessid(goodsSell.getBusinessid());
                     option.setGoodssellid(goodsSell.getId());
                     option.setBusinessname(goodsSell.getBusinessname());
                     if(option.getDeleted()==null){
                         option.setDeleted(false);
                     }
                     runfastGoodsSellOptionService.insertSelective(option);
                     if(option.getSellSubOptionList().size()>0){
                         for(RunfastGoodsSellSubOption subOption:option.getSellSubOptionList()){
                             if(subOption.getId()==null &&  StringUtils.isNotBlank(subOption.getName())){
                                 subOption.setDel(0);
                                 subOption.setBusinessname(option.getBusinessname());
                                 subOption.setBusinessid(option.getBusinessid());
                                 subOption.setGoodssellid(option.getGoodssellid());
                                 subOption.setOptionid(option.getId());
                                 runfastGoodsSellSubOptionService.insert(subOption);
                             }
                         }
                     }
                 }
             }
        }
        if(optionList.size()>0){
           for(RunfastGoodsSellOption option:optionList){
               option.setDeleted(true);
               runfastGoodsSellOptionService.updateByPrimaryKeySelective(option);
               if(option.getSellSubOptionList().size()>0){
                   for(RunfastGoodsSellSubOption subOption:option.getSellSubOptionList()){
                       subOption.setDel(1);
                       runfastGoodsSellSubOptionService.updateByPrimaryKeySelective(subOption);
                   }
               }
           }
        }
         mapper.updateByPrimaryKeySelective(t);
        return Result.ok(null);
    }

    public Map<Object,Object> selectBygoodsId(RunfastGoodsSell t){
        Map<Object,Object> map=new HashMap<Object, Object>();
        Map<Object,Object> map1=new HashMap<Object, Object>();
        map1.put("goodssellid",t.getId());
        map1.put("businessid",t.getBusinessid());
        List<RunfastGoodsSellStandard> standards=runfastGoodsSellStandardService.selectByMap(map1);
        if(standards.size()>0){
            for(RunfastGoodsSellStandard standard:standards){
                //RunfastActivityTargetExample example1=new RunfastActivityTargetExample();
                //example1.createCriteria().andStandaridEqualTo(standard.getId());
                Map<Object,Object> map2=new HashMap<Object, Object>();
                map2.put("standardId",standard.getId());
                List<RunfastActivityTarget> targetList=runfastActivityTargetService.selectTargetAndActivityByMap(map2);
                if(targetList.size()>0){
                    standard.setActivityTarget(targetList.get(0));
                }
           }
        }
        RunfastGoodsSellOptionExample example1=new RunfastGoodsSellOptionExample();
        example1.createCriteria().andGoodssellidEqualTo(t.getId()).andBusinessidEqualTo(t.getBusinessid()).andDeletedNotEqualTo(true);
        List<RunfastGoodsSellOption> optionList=runfastGoodsSellOptionService.selectByExample(example1);
        if(optionList.size()>0){
            for(RunfastGoodsSellOption option:optionList){
                RunfastGoodsSellSubOptionExample example2=new RunfastGoodsSellSubOptionExample();
                example2.createCriteria().andBusinessidEqualTo(t.getBusinessid()).andGoodssellidEqualTo(t.getId()).andOptionidEqualTo(option.getId()).andDelNotEqualTo(1);
                List<RunfastGoodsSellSubOption> subOptionList=runfastGoodsSellSubOptionService.selectByExample(example2);
                option.setSellSubOptionList(subOptionList);
            }
        }
        map.put("standards",standards);
        map.put("optionList",optionList);
        return map;
    }

    public List<RunfastGoodsSell> setStandardListAndOptionList(List<RunfastGoodsSell> goodsSellList){
        if(goodsSellList.size()>0){
            for(RunfastGoodsSell goodsSell:goodsSellList){
                Map<Object,Object> map =selectBygoodsId(goodsSell);
                goodsSell.setStandardList((List<RunfastGoodsSellStandard>)map.get("standards"));
                goodsSell.setOptionList((List<RunfastGoodsSellOption>)map.get("optionList"));
            }
        }
        return goodsSellList;
    }


    public List<RunfastGoodsSell> selectByExampleAndBusinessWithPageable(Map<Object,Object> map1){
        RunfastGoodsSellMapper runfastGoodsSellMapper=(RunfastGoodsSellMapper)this.mapper;
        return runfastGoodsSellMapper.selectByExampleAndBusinessWithPageable(map1);
    }
    public Integer countByExampleAndBusiness(Map<Object,Object> map1){
        RunfastGoodsSellMapper runfastGoodsSellMapper=(RunfastGoodsSellMapper)this.mapper;
        return runfastGoodsSellMapper.countByExampleAndBusiness(map1);
    }

    public List<RunfastGoodsSell> selectByMap(Map<Object,Object> map){
        RunfastGoodsSellMapper runfastGoodsSellMapper=(RunfastGoodsSellMapper)this.mapper;
        return runfastGoodsSellMapper.selectByMap(map);
    }

}